package com.mst.controller;

import com.mst.core.Result;
import com.mst.domain.Book;
import com.mst.service.IBookService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/book")
@AllArgsConstructor
public class BookController {
    private final IBookService bookService;

    @PostMapping("/upload")
    public Result<String> update(MultipartFile file) {
//        原本文件名
        String fileName = file.getOriginalFilename();
//        截取文件的后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        List<String> supperSuffix = Arrays.asList(".png", ".jpg");
        if (!supperSuffix.contains(suffix.toLowerCase())) {
            return Result.error(500, "只能上传图片");
        }
        // 需要把文件保存到本地
//        try (
//                InputStream inputStream = file.getInputStream();
//                BufferedInputStream bis = new BufferedInputStream(inputStream);
//                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\TiANLIANG\\javaWeb\\mst-homework\\profile\\" + file.getOriginalFilename()))
//        ) {
//            byte[] buffer = new byte[1024];
//            int len = 0;
//            while ((len = bis.read(buffer)) != -1) {
//                bos.write(buffer, 0, len);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return Result.error(500, "文件上传异常，请联系管理员");
//        }
        try {
            file.transferTo(new File("E:\\TiANLIANG\\javaWeb\\mst-homework\\profile\\" + file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(500, "文件上传异常，请联系管理员");
        }
        return Result.success(file.getOriginalFilename());
    }

    @PostMapping("/uploads")
    public Result<String> updates(MultipartFile[] files) {
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            // 先做格式的校验
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            List<String> supportSuffix = Arrays.asList(".png", ".jpg");
            if (!supportSuffix.contains(suffix.toLowerCase())) {
                return Result.error(500, "只支持png和jpg格式");
            }
            try {
                file.transferTo(new File("E:\\TiANLIANG\\javaWeb\\mst-homework\\profile\\" + file.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
                return Result.error(500, "文件上传异常，请联系管理员");
            }
        }
        return Result.success("上传成功");
    }

    @PostMapping
    public Result<Integer> addBook(@RequestBody Book book) {
        return Result.success(bookService.addBook(book));
    }

    @DeleteMapping
    public Result<Integer> deleteBook(int id) {
        return Result.success(bookService.deleteBook(id));
    }

    @PutMapping
    public Result<Integer> updateBook(@RequestBody Book book) {
        if (book.getId() == null) {
            return Result.error(400, "请传入id");
        }
        return Result.success(bookService.updateBook(book));
    }

    @GetMapping("/{id}")
    public Result<Book> getBook(@PathVariable int id) {
        return Result.success(bookService.getBookById(id));
    }

    @PostMapping("/list")
    public Result<List<Book>> getBookList(@RequestBody Book book) {
        return Result.success(bookService.getAllBook(book));
    }

    @PostMapping("/listPage")
    public Result<List<Book>> getBookListPage(@RequestBody Book book, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return Result.success(bookService.getAllBookPage(book, pageNum, pageSize));
    }
}